//
//  AudioViewController.m
//  PublicInfo
//
//  Created by enghou on 2018/1/19.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "AudioViewController.h"
#import "CWVoiceView.h"
@interface AudioViewController ()

@end

@implementation AudioViewController{
    CWVoiceView *view;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if (!self.fileURL) {
        view = [[CWVoiceView alloc] initWithFrame:CGRectMake(0, self.view.height - 252,self.view.width, 252)];
        [self.view addSubview:view];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleCancel) name:NOTIFY_RECORDTIMENOTENGHOU object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleFile:) name:NOTIFY_RECORDDONE object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(userCancel) name:NOTIFY_RECORDINTERRUPT object:nil];
    }else{
        //播放该音频
    }
    // Do any additional setup after loading the view.
}

-(void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    view.frame = CGRectMake(0, self.view.height - 252,self.view.width, 252);
}

-(void)userCancel{
    [self dismissViewControllerAnimated:NO completion:nil];
}

-(void)handleCancel{
    [self alertSome:@"录音时间太短"];
}

-(void)handleFile:(NSNotification *)not{
    NSURL *url = not.object;
    if ([url isKindOfClass:[NSURL class]]) {
        if ([self.delegate respondsToSelector:@selector(handleAudioWithFilePath:)]) {
            [self.delegate handleAudioWithFilePath:url];
            [self dismissViewControllerAnimated:NO completion:nil];
        }else{
            [[NSFileManager defaultManager]removeItemAtURL:url error:nil];
            [self dismissViewControllerAnimated:NO completion:nil];
        }
    }
}

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self dismissViewControllerAnimated:NO completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
